﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArticleManagementSystem
{
    public partial class Mainwebsite : Form
    {
        public Mainwebsite()
        {
            InitializeComponent();
        }



        private void Mainwebsite_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetData(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Author like '%{0}%'", name);
            var dt = DbHelper.GetData(sql);
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Addwindow addwindow = new Addwindow();
            var res = addwindow.ShowDialog();
            if (res==DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                var dt = DbHelper.GetData(sqlstu);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var zhuanCreatedTime = CreatedTime.ToString();
                Addwindow addwindow = new Addwindow(id,Title,Author, zhuanCreatedTime);
                var res=addwindow.ShowDialog();
                if (res == DialogResult.Yes)
                { 
                    var name = textBox1.Text;
                    var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                    var dt = DbHelper.GetData(sqlstu);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("失败！");
                }
            }
            else
            {
                MessageBox.Show("你没选择行！");
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var nww = dataGridView1.SelectedRows[0];
                var id = (int)nww.Cells["Id"].Value;
                var Deleteprompt = MessageBox.Show("你确定删除嘛？"
                    ,"提示", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information); 
                if (Deleteprompt.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}",id);
                    var nwwass = DbHelper.AddorUpdateOrDelete(sql);
                    if (nwwass>0)
                    {
                        var name = textBox1.Text;
                        var sqlstu = string.Format("select * from Articles where Author like '%{0}%'",name);
                        var dt = DbHelper.GetData(sqlstu);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功");
                    }
                }
            }
            else
            {
                MessageBox.Show("你没选择行，请选择一条行");
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
